/**
 * 服务分类
 *
 * 文件命名格式：zmn.模块.表/功能.jas
 * @author duanzuocai
 * @since 2018/10/18
 */
layui.define(['form', 'layedit', 'upload', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var layedit = layui.layedit;
    var laydate = layui.laydate;
    let upload = layui.upload;
    let timestamp = new Date().getTime();

    form.render(null, "zmn-form");

    reLoadImage("showHomePageIcon", json.servCategory.showHomePageIcon, "showHomePageIconValue");
    reLoadImage("showCategoryPagePicture", json.servCategory.showCategoryPagePicture, "showCategoryPagePictureValue");

    //初始化文件上传插件
    uploaderImage("showHomePageIcon", "showHomePageIconValue", "png", 1024);
    uploaderImage("showCategoryPagePicture", "showCategoryPagePictureValue", "png", 2048);

    // 自定义验证规则
    form.verify({
        showName: function(value){
            if(!/^[A-Za-z0-9\u4e00-\u9fa5]+$/.test(value)){
                return "分类名称只能输入字母、数字、汉字";
            }
            if(value.length > 20){ return "分类名称长度超过20了，请调整后再试"; };
        },
        sort: function(value){
            if(value != null && value != ''){
                if (!/^[1-9]\d*$/.test(value)) {
                    return "请输入1-10000的正整数，排序号越小显示越靠前";
                }
                if (value > 10000){
                    return "请输入1-10000的正整数，排序号越小显示越靠前";
                }
            }
        },
        simpleSpell: function (value) {
            if (value != null && value != ""){
                var flag = /^[A-Za-z]{1,10}$/.test(value);
                if (!flag){
                    return "请输入英文字母";
                }

                if (value.length > 10){
                    return "简称字段长度不能超过10位";
                }
            }
        },
        tariffName:function (value) {
        if(!/^[A-Za-z0-9\u4e00-\u9fa5]+$/.test(value)){
            return "计价器名称只能输入字母、数字、汉字";
        }
    }
    });

    zmnLayUtil.layFormVerify(form);

    // 提交
    form.on('submit(zmn-form-submit)', function(obj){

        if (obj.field.showName.length > 45){
            layer.alert("分类名称长度超过45了，请调整后再试");
            return false;
        }
        if(!obj.field.tariffStatus){
            layer.alert("计价器状态必选");
            return false;
        }

        var openId = layer.open({type: 3});
        var data = obj.field;
        var url = "/serv/showCateg/addEdit.action";

        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            beforeSend: function () {
                index = layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            },
            success: function (json) {
                parent.layer.msg(json.message,{time: 1000},()=>{
                    location.reload();
                });
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            },
            complete:function () {
                layer.close(index);
            }
        })
        return false;
    });

    /**
     * 描述: 图片上传方法
     * @param elemId
     * @param valueId
     */
    let indexUploadImags;

    function uploaderImage(elemId, valueId, exts, size) {
        upload.render({
            elem: '#' + elemId
            , exts: exts
            , acceptMime: 'image/*'
            , accept: 'images'
            , url: '/product/picture/uploaderImage.action?timestamp=' + timestamp + '&elemId=' + elemId
            , size: size
            , before: function (obj) {
                indexUploadImags = layui.layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            }
            , done: function (res) {
                if (res.code == 2) {
                    var data = res.data;
                    $("#" + valueId).val(data.src);
                    $("#" + elemId).attr("style", "background:url( " + data.src + "?" + Date.now() + ");background-size:100% 100%;background-repeat:no-repeat;");
                    let next = $("#" + valueId).next();
                    let next1 = $(next).next();
                    $(next).attr("style", "visibility:hidden !important");
                    $(next1).attr("style", "visibility:hidden !important");
                } else {
                    layer.msg("上传失败！")
                }
                layui.layer.close(indexUploadImags);
            }
        });
    }

    function reLoadImage(elemId, value, valueId) {
        zmnUploadUtil.reLoadImage($, elemId, value, valueId);
    }

    //点击取消
    // $(document).on('click', '#cancel', function () {
    //     let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    //     parent.layer.close(index);
    // });

    //对外暴露的接口
    exports('art_serv_categ_show_edit', {});
});

function deleteImg(element) {
    var elementValue = element+'Value';
    document.getElementById(element).removeAttribute('style');
    Array.from(document.getElementById(element).children).forEach(function(item){
        item.removeAttribute('style');
    });
    document.getElementById(elementValue).removeAttribute('value');
}